package cn.usian.controller;

import cn.usian.feign.UserClient;
import cn.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("user")
public class ConsumerController {
    //从Nacos注册中心获取服务端的ip、端口、要调用的服务
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    //访问Rest服务的客户端
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

//    @GetMapping(value = "consumer/user/{id}",method = RequestMethod.GET)
//    public User getUserBy(@PathVariable Integer id){
//        ServiceInstance provider = loadBalancerClient.choose("nacos_provider");
//        //指定要调用的服务
//        String url="http://"+provider.getHost()+":"+provider.getPort()+"/getUserById/"+id;
//        return restTemplate.getForObject(url,User.class);
//    }
    @GetMapping("getUserById/{id}")
    public User getUserById(@PathVariable("id") Integer id){
    return userClient.getUserById(id);
}
}
